//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "FwdToCotSwapAdapter.h"
using namespace Cephei::QL::Models::Marketmodels::Models;
#include <gen/QL/Models/Marketmodels/EvolutionDescription.h>
#include <gen/QL/Models/Marketmodels/MarketModel.h>
#include <gen/QL/Math/Matrix.h>
using namespace Cephei::QL::Models::Marketmodels;
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapter::CFwdToCotSwapAdapter (Cephei::QL::Models::Marketmodels::IMarketModel^ forwardModel) : CMarketModel(CFwdToCotSwapAdapter::typeid)
{
    CMarketModel^ _CforwardModel;
    try
    {
#ifdef HANDLE
        _phFwdToCotSwapAdapter = NULL;
#endif
        _CforwardModel = safe_cast<CMarketModel^> (forwardModel);
        _CforwardModel->Lock();
        boost::shared_ptr<QuantLib::MarketModel>& _forwardModel = static_cast<boost::shared_ptr<QuantLib::MarketModel>&> (_CforwardModel->GetShared ()); 
        _ppFwdToCotSwapAdapter = new boost::shared_ptr<QuantLib::FwdToCotSwapAdapter> (new QuantLib::FwdToCotSwapAdapter ( _forwardModel ));
        SetMarketModel (boost::dynamic_pointer_cast<QuantLib::MarketModel> (*_ppFwdToCotSwapAdapter));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CforwardModel != nullptr) _CforwardModel->Unlock();
    }
}
Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapter::CFwdToCotSwapAdapter (boost::shared_ptr<QuantLib::FwdToCotSwapAdapter>& childNative, Object^ owner) : CMarketModel(CFwdToCotSwapAdapter::typeid)
{
#ifdef HANDLE
	_phFwdToCotSwapAdapter = NULL;
#endif
	_ppFwdToCotSwapAdapter = &childNative;
    _ppMarketModel = new boost::shared_ptr<QuantLib::MarketModel> (boost::dynamic_pointer_cast<QuantLib::MarketModel> (*_ppFwdToCotSwapAdapter));
}
Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapter::CFwdToCotSwapAdapter (QuantLib::FwdToCotSwapAdapter& childNative, Object^ owner) : CMarketModel(CFwdToCotSwapAdapter::typeid)
{
#ifdef HANDLE
	_phFwdToCotSwapAdapter = NULL;
#endif
	_ppFwdToCotSwapAdapter = new boost::shared_ptr<QuantLib::FwdToCotSwapAdapter> (&childNative);
    _ppMarketModel = new boost::shared_ptr<QuantLib::MarketModel> (boost::dynamic_pointer_cast<QuantLib::MarketModel> (*_ppFwdToCotSwapAdapter));
    _FwdToCotSwapAdapterOwner = owner;
    _MarketModelOwner = owner;
}

Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapter::CFwdToCotSwapAdapter (CFwdToCotSwapAdapter^ copy) : CMarketModel(CFwdToCotSwapAdapter::typeid)
{
#ifdef HANDLE
	_phFwdToCotSwapAdapter = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFwdToCotSwapAdapter = new boost::shared_ptr<QuantLib::FwdToCotSwapAdapter> (copy->GetShared());
        _ppMarketModel = new boost::shared_ptr<QuantLib::MarketModel> (boost::dynamic_pointer_cast<QuantLib::MarketModel> (*_ppFwdToCotSwapAdapter));
    }
}
Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapter::CFwdToCotSwapAdapter (System::Type^ t) : CMarketModel(CFwdToCotSwapAdapter::typeid)
{
#ifdef HANDLE
	_phFwdToCotSwapAdapter = NULL;
#endif
	if (!t->IsSubclassOf(CFwdToCotSwapAdapter::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapter::CFwdToCotSwapAdapter (QuantLib::Handle<QuantLib::FwdToCotSwapAdapter>& childNative, Object^ owner)  : CMarketModel(CFwdToCotSwapAdapter::typeid)
{
	_phFwdToCotSwapAdapter = &childNative;
	_ppFwdToCotSwapAdapter = &static_cast<boost::shared_ptr<QuantLib::FwdToCotSwapAdapter>>(childNative.currentLink());
    _ppMarketModel = new boost::shared_ptr<QuantLib::MarketModel> (boost::dynamic_pointer_cast<QuantLib::MarketModel> (*_ppFwdToCotSwapAdapter));
    _FwdToCotSwapAdapterOwner = owner;
}
Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapter::CFwdToCotSwapAdapter (QuantLib::Handle<QuantLib::FwdToCotSwapAdapter> childNative)  : CMarketModel(CFwdToCotSwapAdapter::typeid)
{
	_phFwdToCotSwapAdapter = &childNative;
	_ppFwdToCotSwapAdapter = &static_cast<boost::shared_ptr<QuantLib::FwdToCotSwapAdapter>>(childNative.currentLink());
    _ppMarketModel = new boost::shared_ptr<QuantLib::MarketModel> (boost::dynamic_pointer_cast<QuantLib::MarketModel> (*_ppFwdToCotSwapAdapter));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapter::CFwdToCotSwapAdapter (QuantLib::FwdToCotSwapAdapter childNative)  : CMarketModel(CFwdToCotSwapAdapter::typeid)
{
#ifdef HANDLE
	_phFwdToCotSwapAdapter = NULL;
#endif
	_ppFwdToCotSwapAdapter = new boost::shared_ptr<QuantLib::FwdToCotSwapAdapter> (new QuantLib::FwdToCotSwapAdapter (childNative));
    _ppMarketModel = new boost::shared_ptr<QuantLib::MarketModel> (boost::dynamic_pointer_cast<QuantLib::MarketModel> (*_ppFwdToCotSwapAdapter));
}
#endif

Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapter::~CFwdToCotSwapAdapter ()
{
    if (_ppFwdToCotSwapAdapter != NULL)
    {
	    delete _ppFwdToCotSwapAdapter;
        _ppFwdToCotSwapAdapter = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapter::!CFwdToCotSwapAdapter ()
{
    if (_ppFwdToCotSwapAdapter != NULL)
    {
	    delete _ppFwdToCotSwapAdapter;
    }
}
QuantLib::FwdToCotSwapAdapter& Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapter::GetReference ()
{
    if (_ppFwdToCotSwapAdapter == NULL) throw gcnew NativeNullException ();
	return **_ppFwdToCotSwapAdapter;
}
boost::shared_ptr<QuantLib::FwdToCotSwapAdapter>& Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapter::GetShared ()
{
    if (_ppFwdToCotSwapAdapter == NULL) throw gcnew NativeNullException ();
	return *_ppFwdToCotSwapAdapter;
}
QuantLib::FwdToCotSwapAdapter* Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapter::GetPointer ()
{
    if (_ppFwdToCotSwapAdapter == NULL) throw gcnew NativeNullException ();
	return &**_ppFwdToCotSwapAdapter;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FwdToCotSwapAdapter>& Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapter::GetHandle ()
{
	if (_phFwdToCotSwapAdapter == NULL)
	{
		_phFwdToCotSwapAdapter = new Handle<QuantLib::FwdToCotSwapAdapter> (*_ppFwdToCotSwapAdapter);
	}
	return *_phFwdToCotSwapAdapter;
}
#endif
bool Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapter::HasNative () 
{
	return (_ppFwdToCotSwapAdapter != NULL);
}

Cephei::IVector<Double>^ Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapter::Displacements::get ()
{
    try
    {
    	std::vector<QuantLib::Spread>& _rv = (std::vector<QuantLib::Spread>&)(*_ppFwdToCotSwapAdapter)->displacements ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::IEvolutionDescription^ Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapter::Evolution::get ()
{
    try
    {
    	QuantLib::EvolutionDescription& _rv = (QuantLib::EvolutionDescription&)(*_ppFwdToCotSwapAdapter)->evolution ( );   
        Cephei::QL::Models::Marketmodels::CEvolutionDescription^ _nrv = gcnew Cephei::QL::Models::Marketmodels::CEvolutionDescription (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Double>^ Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapter::InitialRates::get ()
{
    try
    {
    	std::vector<QuantLib::Rate>& _rv = (std::vector<QuantLib::Rate>&)(*_ppFwdToCotSwapAdapter)->initialRates ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapter::NumberOfFactors::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppFwdToCotSwapAdapter)->numberOfFactors ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapter::NumberOfRates::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppFwdToCotSwapAdapter)->numberOfRates ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapter::NumberOfSteps::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppFwdToCotSwapAdapter)->numberOfSteps ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IMatrix^ Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapter::PseudoRoot (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i);
    	QuantLib::Matrix& _rv = (QuantLib::Matrix&)(*_ppFwdToCotSwapAdapter)->pseudoRoot ( _i );   
        Cephei::QL::Math::CMatrix^ _nrv = gcnew Cephei::QL::Math::CMatrix (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Models::IFwdToCotSwapAdapter^ Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapter_Factory::Create (Cephei::QL::Models::Marketmodels::IMarketModel^ forwardModel)
{
    return gcnew CFwdToCotSwapAdapter ( forwardModel);
}
